package com.wm.exam.controller;

import com.github.pagehelper.PageInfo;
import com.wm.exam.common.Const;
import com.wm.exam.common.ServerResponse;
import com.wm.exam.pojo.*;
import com.wm.exam.service.*;
import com.wm.exam.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private TimeService timeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ScopeService scopeService;
    @Autowired
    private CategoryVoService categoryVoService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 跳转到添加试题界面
     */
    @RequestMapping("page/paper")
    public String paperPage(HttpServletRequest request, Model model) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        if (user.getType() != 1) {
            return "404";
        }
        // 查询时间
        List<Time> times = timeService.getByStatus();
        model.addAttribute("times", times);
        // 查询专业
        List<Major> majors = majorService.getByStatus();
        model.addAttribute("majors", majors);
        // 查询分数
        List<Scope> scopes = scopeService.getByStatus();
        model.addAttribute("scopes", scopes);
        return "add";
    }

    /**
     * 添加试题
     */
    @PostMapping("paper/addPaper")
    @ResponseBody
    public ServerResponse addPaper(Paper paper, HttpServletRequest request) {
        int result = paperService.addPaper(paper);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("添加成功");
        }

        return ServerResponse.createByErrorMessage("添加失败");
    }

    /**
     * 查看试题列表
     */
    @RequestMapping("paper/list")
    public String paperList(HttpServletRequest request,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize,
                            Model model) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        if (user.getType() != 1) {
            return "404";
        }
        PageInfo<CategoryVo> categoryVos = categoryVoService.getAll(pageNum, pageSize);
        model.addAttribute("categoryVos", categoryVos);
        return "categoryList";
    }

    /**
     * 根据UUID查询试卷内容
     */
    @RequestMapping("paper/paperDetail")
    public String paperDetail(@RequestParam("UUID") String UUID,
                              HttpServletRequest request,
                              Model model) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        if (user.getType() != 1) {
            return "404";
        }
        Category category = categoryService.getByUUID(UUID);
        model.addAttribute("category", category);
        // 查询时间
        List<Time> times = timeService.getByStatus();
        model.addAttribute("times", times);
        // 查询专业
        List<Major> majors = majorService.getByStatus();
        model.addAttribute("majors", majors);
        // 查询分数
        List<Scope> scopes = scopeService.getByStatus();
        model.addAttribute("scopes", scopes);
        List<Paper> papers = paperService.getByPaperUUID(UUID);
        model.addAttribute("list", papers);

        // 得出总题目数
        model.addAttribute("total", papers.size());
        //计算每道题的分数
        Scope scope = scopeService.getByScopeValue(category.getScope());
        int scopeValue = (int) (scope.getScopeValue() / papers.size());
        model.addAttribute("scopeValue", scopeValue);
        model.addAttribute("totalValue", scope.getScopeValue());
        return "paperDetail";
    }

    /**
     * 更新试题内容
     */
    @PostMapping("paper/updatePaper")
    @ResponseBody
    public ServerResponse updatePaper(Paper paper) {
        int result = paperService.updateById(paper);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 删除试题内容
     */
    @RequestMapping("paper/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("deleteId") Integer deleteId) {
        int result = paperService.deleteById(deleteId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
}
